package unitTest.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import unitTest.entity.TestResult;

public class TestRunner extends Thread{
	private List testResults;
	private int caseNum = 0;
	private String[] suiteNames = null;
	
	public void init(int caseNum, String[] suiteNames){
		testResults= new ArrayList();
		this.suiteNames = suiteNames;
		this.caseNum = caseNum;
	}
	
	/**
	 * invoke the server-side test tool 
	 */
	public void run(){
		// simulate the running test tool
		for (int j = 0; j < suiteNames.length; j++) {
			for (int i = 0; i < caseNum; i++) {
				TestResult tr = new TestResult();
				tr.setName(suiteNames[j] + "-case-" + i);
				tr.setStart(new Date().toString());
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				tr.setEnd(new Date().toString());
				tr.setResult(genRandomResult());
				testResults.add(tr);
			}
		}
	}
	
	/**
	 * simulate test results
	 * @return
	 */
	private String genRandomResult(){
		int result = new Random().nextInt(3);
		return result==1?"FAIL":"PASS";
	}
	
	/**
	 * read the dynamic test results (such as result log file)
	 */
	public List getTestResults(){
		return this.testResults;
	}
}
